{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0be423e9-1808-4a60-823b-db2046533db0",
   "metadata": {},
   "source": [
    "https://platform.openai.com/docs/api-reference/completions/create\n",
    "\n",
    "https://api2d.com/wechat/recharge\n",
    "\n",
    "https://api2d-doc.apifox.cn/\n",
    "\n",
    "- https://openai.api2d.net 正式接口，分布式部署\n",
    "- https://oa.api2d.net 备用接口，如果上一个无法使用（比如因为域名包含 openai 被屏蔽）可以使用这个\n",
    "- https://oa.api2d.site，https://oa.api2d.online 临时域名，如果前两个都无法访问可以尝试这两个（过期时间 2024-06-15）\n",
    "- https://stream.api2d.net 备用接口，如果前两个都无法使用，可以使用这个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e916505a-38e5-40c3-84ed-d522767da8c1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "url = \"https://oa.api2d.site/v1/chat/completions\"\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    \n",
    "    # <-- 把 fkxxxxx 替换成你自己的 Forward Key，注意前面的 Bearer 要保留，并且和 Key 中间有一个空格\n",
    "    'Authorization': 'Bearer fk212271-dza4SUgOeIGfyMpwKi32tnP9tKPKWopL',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39c2d38f-54a8-4c01-b781-74a7881f2897",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JSON Response  {'id': 'chatcmpl-8YJ5SigVeuZH70CcqQSJwMUI3b6HR', 'object': 'chat.completion', 'created': 1703187214, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': \"| prompt                                                       | A                                                            | B                                                            | C                                                            | D                                                            | E                                                            | answer |\\n| :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----- |\\n| According to the text, what is one simple statement of the second law of thermodynamics? | Heat always moves from hotter objects to colder objects unless energy is supplied to reverse the direction of heat flow | Heat always moves from colder objects to hotter objects unless energy is supplied to reverse the direction of heat flow | All heat energy can be converted into work in a cyclic process | Heat always moves from objects of equal temperature unless energy is supplied to reverse the direction of heat flow | Heat can be converted into work in a cyclic process without the need for energy input | A |\\n| What does the second law of thermodynamics establish as a physical property of a thermodynamic system? | Entropy | Temperature | Pressure | Internal energy | Work | A |\\n| Which scientist formulated Carnot's theorem, which provides an upper limit for the efficiency of conversion of heat to work in a heat engine? | Sadi Carnot | Rudolf Clausius | Isaac Newton | James Clerk Maxwell | Albert Einstein | A |\\n| In terms of the concept of thermodynamic temperature, which law is relied upon by the second law of thermodynamics? | Zeroth law of thermodynamics | First law of thermodynamics | Third law of thermodynamics | Fourth law of thermodynamics | None of the above | A |\\n| According to the text, what is the role of the second law of thermodynamics in predicting processes? | It provides necessary criteria for spontaneous processes | It determines the conservation of energy in a system | It defines the concept of entropy | It establishes the irreversible nature of natural processes | None of the above | A |\"}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 673, 'completion_tokens': 369, 'total_tokens': 1042, 'pre_token_count': 4096, 'pre_total': 48, 'adjust_total': 39, 'final_total': 9}, 'system_fingerprint': None}\n",
      "JSON Response  {'id': 'chatcmpl-8YJ5ZfpJt769ZCcip2nwRQy3UYmaj', 'object': 'chat.completion', 'created': 1703187221, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '| prompt                                                       | A                                                            | B                                                            | C                                                            | D                                                            | E                                                            | answer |\\n| :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----- |\\n| What is the second law of thermodynamics?                     | Heat always moves from hotter objects to colder objects       | Heat always moves from colder objects to hotter objects       | Heat remains constant in a thermodynamic system                | Heat can be converted into work in a cyclic process           | Heat cannot be converted into work in a cyclic process        | A      |\\n| Which property does the second law of thermodynamics establish? | Energy                                                       | Entropy                                                      | Temperature                                                  | Mass                                                         | Pressure                                                     | B      |\\n| According to the second law of thermodynamics, what happens to the entropy of isolated systems? | It decreases                                                | It increases                                                | It remains constant                                          | It fluctuates                                                | It becomes negative                                          | B      |\\n| How was the second law of thermodynamics historically accepted? | Through empirical findings                                   | Through rigorous mathematical proofs                         | Through experimental demonstrations                          | Through theoretical calculations                            | Through observations and measurements                        | A      |\\n| Who formulated the first rigorous definition of the second law of thermodynamics? | Sadi Carnot                                                  | Rudolf Clausius                                              | Nikola Tesla                                                 | James Watt                                                   | Isaac Newton                                                 | B      |'}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 673, 'completion_tokens': 284, 'total_tokens': 957, 'pre_token_count': 4096, 'pre_total': 48, 'adjust_total': 40, 'final_total': 8}, 'system_fingerprint': None}\n",
      "JSON Response  {'id': 'chatcmpl-8YJ5e8GFWFdxQMBK2bNaT6yDOcNiv', 'object': 'chat.completion', 'created': 1703187226, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '| Which of the following statements accurately describes the second law of thermodynamics? | Heat always moves from colder objects to hotter objects.   | Heat always moves from hotter objects to colder objects.   | Heat can move in any direction between objects of different temperatures.   | Heat only moves between objects of the same temperature.   | Heat is never transferred between objects.   | B      |\\n| What is the second law of thermodynamics based on?                  | Conservation of energy   | Universal experience concerning heat and energy interconversions   | Probability distributions of atomic states   | The concept of entropy   | The first law of thermodynamics   | B      |\\n| How is the concept of entropy defined by the second law of thermodynamics? | Entropy is the measure of disorder in a thermodynamic system   | Entropy is the measure of temperature in a thermodynamic system   | Entropy describes the flow of heat between objects   | Entropy determines the direction of heat flow in a thermodynamic system   | Entropy determines the energy conversion in a thermodynamic system   | A      |\\n| What does the second law of thermodynamics state about the entropy of isolated systems? | The entropy of isolated systems always decreases   | The entropy of isolated systems always remains constant   | The entropy of isolated systems cannot decrease   | The entropy of isolated systems increases over time   | The entropy of isolated systems depends on their internal energy   | C      |\\n| Who formulated the first rigorous definition of the second law of thermodynamics? | Sadi Carnot   | Rudolf Clausius   | German scientist   | French scientist   | Universal experience   | B      |'}, 'logprobs': None, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 673, 'completion_tokens': 333, 'total_tokens': 1006, 'pre_token_count': 4096, 'pre_total': 48, 'adjust_total': 39, 'final_total': 9}, 'system_fingerprint': None}\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"model\": \"gpt-3.5-turbo\",\n",
    "#     \"max_tokens\": 20, # 返回的token 最大个数\n",
    "#     \"temperature\": 0.2 0.8, # 0-2 约靠近0返回结果质量越高，随机性越小\n",
    "    \n",
    "    \"messages\": [{\"role\": \"user\", \"content\": \n",
    "\"\"\"\n",
    "\n",
    "Based on the text below, can you generate a list of 5 different multiple choice questions whom answers should be in the given text and that has 5 choices each ?  No other content is required, give the response with template. You only need to answer the required results, and you don't need to output other content.\n",
    "\n",
    "TEXT:\n",
    "The second law of thermodynamics is a physical law based on universal experience concerning heat and energy interconversions. One simple statement of the law is that heat always moves from hotter objects to colder objects (or \"downhill\"), unless energy in some form is supplied to reverse the direction of heat flow. Another definition is: \"Not all heat energy can be converted into work in a cyclic process.\"[1][2][3]\n",
    "\n",
    "The second law of thermodynamics in other versions establishes the concept of entropy as a physical property of a thermodynamic system. It can be used to predict whether processes are forbidden despite obeying the requirement of conservation of energy as expressed in the first law of thermodynamics and provides necessary criteria for spontaneous processes. The second law may be formulated by the observation that the entropy of isolated systems left to spontaneous evolution cannot decrease, as they always arrive at a state of thermodynamic equilibrium where the entropy is highest at the given internal energy.[4] An increase in the combined entropy of system and surroundings accounts for the irreversibility of natural processes, often referred to in the concept of the arrow of time.[5]\n",
    "\n",
    "Historically, the second law was an empirical finding that was accepted as an axiom of thermodynamic theory. Statistical mechanics provides a microscopic explanation of the law in terms of probability distributions of the states of large assemblies of atoms or molecules. The second law has been expressed in many ways. Its first formulation, which preceded the proper definition of entropy and was based on caloric theory, is Carnot's theorem, formulated by the French scientist Sadi Carnot, who in 1824 showed that the efficiency of conversion of heat to work in a heat engine has an upper limit.[6][7] The first rigorous definition of the second law based on the concept of entropy came from German scientist Rudolf Clausius in the 1850s and included his statement that heat can never pass from a colder to a warmer body without some other change, connected therewith, occurring at the same time.\n",
    "\n",
    "The second law of thermodynamics allows the definition of the concept of thermodynamic temperature, relying also on the zeroth law of thermodynamics.\n",
    "\n",
    "OUTPUT Template in markwdown table:\n",
    "\n",
    "| prompt                                                       | A                                                            | B                                                            | C                                                            | D                                                            | E                                                            | answer |\n",
    "| :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- | :----- |\n",
    "| Which of the following statements accurately describes the impact of Modified Newtonian Dynamics ... | MOND is a theory that reduces the observed missing baryonic... | MOND is a theory that increases the discrepancy between the observed missing baryonic mass... | MOND is a theory that explains the missing baryonic mass in galaxy clusters... | MOND is a theory that reduces the discrepancy between the observed missing baryonic mass... | MOND is a theory that eliminates the observed missing baryonic mass in galaxy... | D      |\n",
    "\"\"\"}]\n",
    "}\n",
    "\n",
    "for _ in range(3):\n",
    "    response = requests.post(url, headers=headers, json=data)\n",
    "    print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a622beb6-7553-4d0e-86eb-1966a14e6043",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JSON Response  {'id': 'chatcmpl-8IF9FIZyLnhIS9WOUzaYFIgvtJhlg', 'object': 'chat.completion', 'created': 1699358825, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': \"Today, Xiao Wang and I went to Zhao Si's house to have a meal.\"}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 60, 'completion_tokens': 17, 'total_tokens': 77, 'pre_token_count': 4096, 'pre_total': 42, 'adjust_total': 41, 'final_total': 1}}\n",
      "JSON Response  {'id': 'chatcmpl-8IF9GFOTQ02EMaa4gyophWW2h0ubQ', 'object': 'chat.completion', 'created': 1699358826, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': \"Today, I went to Zhao Si's house for a meal with Xiao Wang.\"}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 60, 'completion_tokens': 16, 'total_tokens': 76, 'pre_token_count': 4096, 'pre_total': 42, 'adjust_total': 41, 'final_total': 1}}\n",
      "JSON Response  {'id': 'chatcmpl-8IF9IyVPocAiVvPMzuMmNW0ekLSra', 'object': 'chat.completion', 'created': 1699358828, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': \"Today I went to Zhao Si's house for dinner with Xiao Wang.\"}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 60, 'completion_tokens': 14, 'total_tokens': 74, 'pre_token_count': 4096, 'pre_total': 42, 'adjust_total': 41, 'final_total': 1}}\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"model\": \"gpt-3.5-turbo\",\n",
    "    # \"max_tokens\": 5,\n",
    "    \"temperature\": 1, \n",
    "    \n",
    "    # 0-2，0随机性越小，可靠性越高。 2 随机数越大，可靠性越低。\n",
    "    # 0 关闭dropout 关闭了随机性 没有随机性 输出结果很简短\n",
    "    # 0.7 - 1\n",
    "    \n",
    "    \"messages\": [{\"role\": \"user\", \"content\": \n",
    "'''\n",
    "请将下面的文本进行文本翻译，从中文翻译为英文。\n",
    "输入文本：今天我和小王一起去赵四家吃饭。\n",
    "'''\n",
    "                 },\n",
    "]\n",
    "}\n",
    "\n",
    "for _ in range(3):\n",
    "    response = requests.post(url, headers=headers, json=data)\n",
    "    print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3ee904ea-95ed-4f30-a80e-2a0a01244ec3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JSON Response  {'id': 'chatcmpl-7okK0kqXcwxA0cyFkC7z6jpFSLTP8', 'object': 'chat.completion', 'created': 1692328816, 'model': 'gpt-3.5-turbo-0613', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '当然！这是一个经典的笑话：\\n\\n有一天，小明去参加一个面试。面试官问他：“小明，你有什么特长吗？”小明回答：“我可以模仿动物的叫声。”面试官觉得很好奇，就说：“好，那你模仿一下狗的叫声吧。”'}, 'finish_reason': 'length'}, {'index': 1, 'message': {'role': 'assistant', 'content': '当然！这是一个经典的笑话：\\n\\n有一天，小明去参加一个面试。面试官问他：“小明，你有什么特长吗？”小明回答：“我可以模仿动物的叫声。”面试官觉得很好奇，就说：“好，那你模仿一下狗的叫声吧。”'}, 'finish_reason': 'length'}, {'index': 2, 'message': {'role': 'assistant', 'content': '当然！这是一个经典的笑话：\\n\\n有一天，小明去参加一个面试。面试官问他：“小明，你有什么特长吗？”小明回答：“我可以模仿动物的叫声。”面试官觉得很好奇，就说：“好，那你模仿一下狗的叫声吧。”'}, 'finish_reason': 'length'}, {'index': 3, 'message': {'role': 'assistant', 'content': '当然！这是一个经典的笑话：\\n\\n有一天，小明去参加一个面试。面试官问他：“小明，你有什么特长吗？”小明回答：“我可以模仿动物的叫声。”面试官觉得很好奇，就说：“好，那你模仿一下狗的叫声吧。”'}, 'finish_reason': 'length'}], 'usage': {'prompt_tokens': 18, 'completion_tokens': 400, 'total_tokens': 418, 'pre_token_count': 100, 'pre_total': 2, 'adjust_total': -3, 'final_total': 5}}\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"model\": \"gpt-3.5-turbo\",\n",
    "    \"max_tokens\": 100,\n",
    "    \"temperature\": 0,\n",
    "    \"n\": 4,\n",
    "    \"messages\": [{\"role\": \"user\", \"content\": \"你好！给我讲个笑话。\"}]\n",
    "}\n",
    "\n",
    "response = requests.post(url, headers=headers, json=data)\n",
    "print(\"JSON Response \", response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "243e9dd4-5797-47f6-b5d1-4d86403657db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
